package com.cn.tianxin.ssserver.controller;

import com.cn.tianxin.ssserver.constants.WeixinConstans;
import com.cn.tianxin.ssserver.service.WeixinService;
import com.cn.tianxin.ssserver.service.WeixinXCXService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/weixinTest")
public class WeixinTestController {

    @Autowired
    WeixinService weixinService;

    @RequestMapping("/verification")
    public String verification(String signature,String timestamp,String nonce,String echostr){

        // 验证消息是否来自微信
        // 1）将token、timestamp、nonce三个参数进行字典序排序 2）将三个参数字符串拼接成一个字符串进行sha1加密 3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信

        String token = WeixinConstans.testAccountToken;
        String[] array = {token, timestamp, nonce};
        Arrays.sort(array);

        System.out.println(array);

        return echostr;
    }

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/getAccessToken")
    public String getAccessToken(){
        return weixinService.getWXAccessToken();
    }

    /**
     * 微信服务号，网页授权回调地址
     * @param code    准备用来解析用户openid
     * @param state    标记该哪一次的关联
     */
    @RequestMapping("/authCallBack")
    public String authCallBack(@RequestParam(required = false) String code,@RequestParam(required = false) String state){
        //微信授权回调页面

        //接受用户网页授权的信息，解析并保存用户openid
        //根据state来确定是哪一次 用户扫码的。    绑定该用户openid到内部系统

        System.out.println("state=>"+state);
        System.out.println("code=>"+code);

        //根据code获取相应的openid

        try {
            Map<String, Object> map = weixinService.getUserOpenId(code);

            if (map!=null){
                //return String.format("对于waterman，你的openid是=>openid:%s",map.get("openid"));
                //return String.format("code转换=>"+map.toString());
                System.out.println("WeixinTestController.authCallBack=>codeMap=>"+map);
                Map<String, Object> wxUserInfo = weixinService.getWXUserInfo(map);

                return "用户信息：\n"+wxUserInfo.toString();
            }else {
                return String.format("获取openid失败 code:%s",code);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return String.format("获取openid失败 code:%s    \nerror:%s",code,e.getMessage());
        }

    }

    //网页授权，验证域名验证域名
    @RequestMapping("/authCallBack/MP_verify_bipw5ptP2ZPqWfOb.txt")
    public String authSeverValid(){
        return "bipw5ptP2ZPqWfOb";
    }

    @RequestMapping("/getUserOpenId")
    public String getUserOpenId(@RequestParam(required = false) String code,@RequestParam(required = false) String state){
        try {
            Map<String, Object> map = weixinService.getUserOpenId(code);

            if (map!=null){
                return String.format("对于waterman，你的openid是=>openid:%s",map.get("openid"));
            }else {
                return String.format("获取openid失败 code:%s",code);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return String.format("获取openid失败 code:%s    \nerror:%s",code,e.getMessage());
        }
    }

    @RequestMapping("/codeToAuthToken")
    public Map<String,Object> codeToAuthToken(String code,String appid,String secret){
        try {
            return weixinService.codeToAuthToken(code,appid,secret);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Autowired
    WeixinXCXService weixinXCXService;

    @RequestMapping("/code2Session")
    public Map<String,Object> code2Session(String code){
        try {
            return weixinXCXService.code2Session(code);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据小程序调用该接口，企业支付
    @RequestMapping("/enterprisePay")
    public Map<String,Object> enterprisePay(String redPackId,String code){
        System.out.println("WeixinTestController.enterprisePay=>redPackId:"+redPackId+"    code:"+code);
        Map<String, Object> map = weixinXCXService.enterprisePay(code, redPackId);
        return map;
    }

    @RequestMapping("/codeToWXXCX/jqSxVJNcTa.txt")
    public String wxxcxQRCodeValid(){
        return "a758412c602a41317215225cc0decb1b";
    }

    @RequestMapping("/codeToWXXCX/{redPackId}")
    public String codeToWXXCX(@PathVariable("redPackId") String redPackId){
        return redPackId;
    }

    @RequestMapping("/jqSxVJNcTa.txt")
    public String wxxcxQRCodeValid2(){
        return "a758412c602a41317215225cc0decb1b";
    }

    @RequestMapping("/codeToWXXCX")
    public String codeToWXXCX2(String redPackId){
        //return redPackId;

        return "this is used for test!";
    }

}
